package cn.pm.datasource.sharding.algorithm;

import lombok.SneakyThrows;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.Collection;

/**
 * Create by spf on 2019/11/29
 *
 * 本分片算法处理器，仅用于根据日期字段动态分表（不分库，且会自动创建不存在的表）的业务处理
 * 需要自动创建表，需在配置文件中指定 spring.shardingsphere.sharding.auto-create-table-config.autoCreateTables相关配置
 */
public class YYYYAutoCreateTablesShardingAlgorithm extends DateBaseAutoCreateTablesShardingAlgorithm implements PreciseShardingAlgorithm, RangeShardingAlgorithm {

    /**
     * 精确分片算法，用于=和IN。。该类需实现PreciseShardingAlgorithm接口并提供无参数的构造器
     */
    @SneakyThrows
    @Override
    public String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue) {
        return super.doShardingPrecise(availableTargetNames, shardingValue, "_yyyy");
    }

    /**
     * 范围分片算法，用于BETWEEN，可选。。该类需实现RangeShardingAlgorithm接口并提供无参数的构造器
     */
    @SneakyThrows
    @Override
    public Collection<String> doSharding(Collection availableTargetNames, RangeShardingValue shardingValue) {
        return super.doShardingRange(availableTargetNames, shardingValue, "_yyyy");
    }
}
